﻿using NServiceBus;
using NServiceBus.Saga;
using MyMessages;

namespace Subscriber1
{
    public class SagaMessageHandler : Saga<SagaHeader>
                                        , IAmStartedByMessages<BeginSagaMessage>
                                        , IHandleMessages<ContinueSagaMessage>
                                        , IHandleMessages<EndSagaMessage>
    {
        public void Handle(BeginSagaMessage message)
        {
            Data.ProcessId = message.ProcessId;
            Data.BeginDate = message.BeginDate;
        }

        public void Handle(ContinueSagaMessage message)
        {
            var child = new SagaUpdates()
                            {
                                SomeMoreInformation = message.SomeMoreInformation,
                                SomeMoreInformation2 = message.SomeMoreInformation2
                            };
            Data.AddChild(child);
        }

        public void Handle(EndSagaMessage message)
        {
            Data.EndDate = message.EndDate;
            Data.CompletionMessage = message.CompletionMessage;
        }

        public override void ConfigureHowToFindSaga()
        {
            ConfigureMapping<ContinueSagaMessage>(t => t.ProcessId, m => m.ProcessId);
            ConfigureMapping<EndSagaMessage>(t => t.ProcessId, m => m.ProcessId);
        }

    }
}
